﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using VDC.COMMON;
using VDC.VONE.ENTITIES;

namespace VDC.VONE.DAL
{
    public class UserDefaultData
    {
        /// <summary>
        /// Insert to db UserDefault
        /// </summary>
        /// <param name="userDefault">Param userDefault</param>
        /// <returns>Return Fail: 0, Success: @@Identity</returns>
        public int Insert(UserDefaultEN userDefault)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.UserDefault.NodeWFRoleDefaultId, userDefault.NodeWFRoleDefaultId),
                                    new SqlParameter(Constants.SqlParamerterName.UserDefault.UserId, userDefault.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.UserDefault.DepartmentId, userDefault.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.UserDefault.PositionsId, userDefault.PositionsId)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserDefaultInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete to db UserDefault
        /// </summary>
        /// <param name="userDefaultId">Param userDefaultId</param>
        public void Delete(int userDefaultId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserDefaultDelete, userDefaultId));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist userDefault
        /// </summary>
        /// <param name="userDefault">Param userDefault</param>
        /// <returns>Return Exist</returns>
        public bool Exist(UserDefaultEN userDefault)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.UserDefault.NodeWFRoleDefaultId, userDefault.NodeWFRoleDefaultId),
                                    new SqlParameter(Constants.SqlParamerterName.UserDefault.UserId, userDefault.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.UserDefault.DepartmentId, userDefault.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.UserDefault.PositionsId, userDefault.PositionsId)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserDefaultExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the type of the user default by node workflow_ to CC_ profile.
        /// </summary>
        /// <param name="nodeWorkflowId">The node workflow id.</param>
        /// <param name="toCc">if set to <c>true</c> [to cc].</param>
        /// <param name="profileTypeId">The profile type id.</param>
        /// <returns>return UserDefaultEN</returns>
        public List<UserDefaultEN> GetUserDefaultByNodeWorkflow_ToCc_ProfileType(int nodeWorkflowId, bool toCc, int profileTypeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                UserDefaultEN userDefault = null;
                var lst = new List<UserDefaultEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserDefaultGetByNodeWorkflowToCcProfileType, nodeWorkflowId, toCc, profileTypeId);
                while (reader.Read())
                {
                    userDefault = new UserDefaultEN();

                    if (!reader.IsDBNull(0))
                    {
                        userDefault.UserDefaultId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        userDefault.NodeWFRoleDefaultId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        userDefault.UserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        userDefault.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        userDefault.PositionsId = reader.GetInt32(4);
                    }

                    lst.Add(userDefault);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lst;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list UserDefaultEN by nodeFWRoleDefaultId
        /// </summary>
        /// <param name="nodeFWRoleDefaultId">param nodeFWRoleDefaultId</param>
        /// <returns>return list UserDefaultEN</returns>
        public List<UserDefaultEN> GetUserDefaultByNodeFWRoleDefaultId(int nodeFWRoleDefaultId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                UserDefaultEN userDefault = null;
                var lst = new List<UserDefaultEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VoneUserDefaultGetByNodeFWRoleDefaultId, nodeFWRoleDefaultId);
                while (reader.Read())
                {
                    userDefault = new UserDefaultEN();

                    if (!reader.IsDBNull(0))
                    {
                        userDefault.UserDefaultId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        userDefault.NodeWFRoleDefaultId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        userDefault.UserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        userDefault.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        userDefault.PositionsId = reader.GetInt32(4);
                    }

                    lst.Add(userDefault);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lst;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

         /// <summary>
        /// Get list UserDefaultEN by nodeWorkflowRollId
        /// </summary>
        /// <param name="nodeWorkflowRollId">param nodeWorkflowRollId</param>
        /// <returns>return list UserDefaultEN</returns>
        public List<UserDefaultEN> GetUserDefaultByNodeWorkflowRollId(int nodeWorkflowRollId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                UserDefaultEN userDefault = null;
                var lst = new List<UserDefaultEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserDefaultGetByNodeWorkflowRollId, nodeWorkflowRollId);
                while (reader.Read())
                {
                    userDefault = new UserDefaultEN();

                    if (!reader.IsDBNull(0))
                    {
                        userDefault.UserDefaultId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        userDefault.NodeWFRoleDefaultId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        userDefault.UserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        userDefault.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        userDefault.PositionsId = reader.GetInt32(4);
                    }

                    lst.Add(userDefault);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lst;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}
